package myapps.ttclient.adapter;

import myapps.ttclient.R;
import myapps.ttclient.connection.Connection;
import myapps.ttclient.connection.ConnectionListManager;
import myapps.ttclient.connection.ConnectionList_Bluetooth;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

public class ConnectionListAdapter_Bluetooth extends BaseAdapter {
	private Context context;
	
	private ConnectionList_Bluetooth connectionList;
	
	private LayoutInflater layoutInflater;
	
	public ConnectionListAdapter_Bluetooth(Context context, 
			ConnectionListManager connectionListManager) {
		this.context = context;
		this.connectionList = connectionListManager.getConnectionList_Bluetooth();
		this.layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	@Override
	public void notifyDataSetChanged() {
		super.notifyDataSetChanged();
	}
	
	public int getCount() {
		return this.connectionList.getCount();
	}
	
	public Connection getItem(int position) {
		return this.connectionList.get(position);
	}
	
	public long getItemId(int position) {
		return position;
	}
	
	public View getView(int position, View convertView, ViewGroup parent) {
		ConnectionViewHolder holder;
		
		if (convertView == null) {
			convertView = this.layoutInflater.inflate(R.layout.connection, null);
			
			holder = new ConnectionViewHolder();
			holder.use = (RadioButton) convertView.findViewById(R.id.use);
			holder.icon = (ImageView) convertView.findViewById(R.id.icon);
			holder.name = (TextView) convertView.findViewById(R.id.name);
			
			convertView.setTag(holder);
		} else {
			holder = (ConnectionViewHolder) convertView.getTag();
		}
		
		Connection connection = this.connectionList.get(position);
		
		holder.use.setChecked(position == this.connectionList.getUsedPosition());
		holder.icon.setImageResource(R.drawable.bluetooth);
		holder.name.setText(connection.getName());
		
		return convertView;
	}
	
	private class ConnectionViewHolder {
		public RadioButton use;
		public ImageView icon;
		public TextView name;
	}
}